package com.hgcode.platform.config.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

@ConfigurationProperties(prefix = "spring.redisson")
@Component
public class RedisConfig extends CachingConfigurerSupport {

    private List<String> address;

    @Bean
    RedissonClient redisson() throws Exception {

        Config config = new Config();
        ClusterServersConfig clusterConfig = config.useClusterServers().
                setScanInterval(2000).
                setKeepAlive(true).
                setMasterConnectionPoolSize(20).
                setMasterConnectionMinimumIdleSize(10).
                setSlaveConnectionPoolSize(20).
                setSlaveConnectionMinimumIdleSize(10);
        clusterConfig.setPingConnectionInterval(3000);
        for (String str : address) {
            clusterConfig.addNodeAddress(str);
        }
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }

    public List<String> getAddress() {
        return address;
    }

    public void setAddress(List<String> address) {
        this.address = address;
    }
}